package io.github.melin.sqlflow.metadata;

import java.util.List;
import java.util.Objects;

import static com.google.common.collect.MoreCollectors.toOptional;

public final class SchemaTable {

    private final String catalogName;

    private final String schemaName;

    private final String tableName;

    private List<String> columns;

    private List<String> partitionColumns=null;  // 新增分区列字段

    public SchemaTable(String catalogName, String schemaName, String tableName, List<String> columns) {
        this.catalogName = catalogName;
        this.schemaName = schemaName;
        this.tableName = tableName;
        this.columns = columns;
    }

    public SchemaTable(String schemaName, String tableName, List<String> columns) {
        this.catalogName = null;
        this.schemaName = schemaName;
        this.tableName = tableName;
        this.columns = columns;
    }

    public SchemaTable(String tableName, List<String> columns) {
        this.catalogName = null;
        this.schemaName = null;
        this.tableName = tableName;
        this.columns = columns;
    }

    public SchemaTable(String schemaName, String tableName, List<String> columns, List<String> partitionColumns) {
        this.catalogName = null;
        this.schemaName = schemaName;
        this.tableName = tableName;
        this.columns = columns;
        this.partitionColumns = partitionColumns;
    }

    // 新增获取分区列的方法
    public List<String> getPartitionColumns() {
        return partitionColumns;
    }

    public void setPartitionColumns(List<String> partitionColumns) {
        this.partitionColumns = partitionColumns;
    }

    public String getCatalogName() {
        return catalogName;
    }

    public String getSchemaName() {
        return schemaName;
    }

    public String getTableName() {
        return tableName;
    }

    public List<String> getColumns() {
        return columns;
    }

    public void setColumns(List<String> columns) {
        this.columns = columns;
    }

    public String getColumn(String name) {
        return columns.stream()
                .filter(column -> column.equals(name))
                .collect(toOptional())
                .orElseThrow(() -> new IllegalArgumentException("Invalid column name: " + name));
    }

    @Override
    public String toString() {
        if (catalogName != null) {
            return catalogName + '.' + schemaName + '.' + tableName;
        } else if (schemaName != null) {
            return schemaName + '.' + tableName;
        } else {
            return tableName;
        }
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        SchemaTable that = (SchemaTable) o;
        return Objects.equals(catalogName, that.catalogName) &&
                Objects.equals(schemaName, that.schemaName) &&
                Objects.equals(tableName, that.tableName) &&
                columns.size() == that.columns.size() &&
                columns.containsAll(that.columns)&&
                Objects.equals(partitionColumns, that.partitionColumns);
    }

    @Override
    public int hashCode() {
        return Objects.hash(catalogName, schemaName, tableName);
    }
}